import { OneModal } from '@/components/one-ui';
import { IOneModalRef } from '@/pages/type';
import { Ref, useContext, useEffect, useRef, useState } from 'react';
import { UpdateContractService, queryContractMaterialList } from '../services/UpdateContractService';
import { contractTableConfig } from '../mconfig/UpdateContractColumns';
import { message } from 'antd';
import { onCloseModalCommon } from '@/utils';
import { showTotalInfo } from '@/common/constVariable';
import { UpdateTableCommon } from './UpdateTableCommon';
import { ToAssetControllerAPI } from '../common/type';
import { IToAssetControllerActionEnum, useToAssetControllerReducer } from './useManageReducer';
import { ToAssetControllerContext } from './useManageContext';

export const ContractModal: React.FC<ToAssetControllerAPI.IOpenMaterialModal> = ({ curRes, onClose, isShowModal }) => {
  /** 弹框的ref */
  const updateModalRef: Ref<IOneModalRef> = useRef(null);
  const { contractList, styleFixCls, contractForm, materielList, toAssetControllerDispatch } = useContext(ToAssetControllerContext);

  /** 获取列表 */
  const { data, params, loading, pagination, searchList, loadMdicMapObj, getManageData } = UpdateContractService(curRes);

  /** 暂时保存选中的值 */
  const [checkMaterialList, setCheckMaterialList] = useState<any>([]);

  /** 搜索 */
  const onSearch = (payload: Record<string, any>) => {
    getManageData({ ...payload });
  };

  /** 重置 */
  const onReset = (payload: Record<string, any>) => {
    getManageData({ ...payload });
  };

  /** 分页change 方法 */
  const onPageChange = (current: number, pageSize: number) => {
    getManageData({ ...params, current, pageSize });
  };

  /** 点击选中 */
  const onSelectTable = (selectInfo: Record<string, any>[]) => {
    setCheckMaterialList(selectInfo);
  };

  /** 初始化 */
  useEffect(() => {
    setCheckMaterialList(contractList);
  }, [isShowModal]);

  /** 点击确定 */
  const onSave = () => {
    if (checkMaterialList && checkMaterialList?.length > 0) {
      toAssetControllerDispatch({
        type: IToAssetControllerActionEnum.SELECT_CONTRACT,
        payload: checkMaterialList,
      });
      onCloseModalCommon(updateModalRef);
    } else {
      message.error('请至少选择一个合同');
    }
    return;
  };

  return (
    <>
      <OneModal
        open={true}
        getContainer={false}
        ref={updateModalRef}
        title={'合同列表'}
        onCancel={onClose}
        width={document.body.clientWidth * 0.65}
        bodyStyle={{
          height: document.body.clientHeight * 0.55,
          padding: 10,
        }}
        onOk={onSave}
      >
        <UpdateTableCommon
          styleFixCls={styleFixCls}
          searchProps={{
            searchList,
            onReset,
            onSearch,
          }}
          selectTablePorps={{
            onSelectTable,
            selectRowObjList: checkMaterialList,
            selectType: 'checkbox',
            setCheckRows: setCheckMaterialList,
          }}
          tableProps={{
            columns: contractTableConfig(),
            loading,
            dataSource: data?.rows,
            pagination,
            onPageChange,
            rowKey: 'id',
            tableTotalShow: (total: any, range: any) => {
              return (
                <div
                  style={{
                    display: 'flex',
                    width: '100%',
                    justifyContent: 'space-between',
                  }}
                >
                  <div>
                    已选择<strong style={{ color: 'red' }}> {checkMaterialList?.length} </strong>条合同
                  </div>
                  <div>{showTotalInfo(total, range)}</div>
                </div>
              );
            },
          }}
        />
      </OneModal>
    </>
  );
};
